package com.lc.activiti.sys.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lc.activiti.config.Result;
import com.lc.activiti.config.TreeSetting;
import com.lc.activiti.sys.dao.SysDeptDao;
import com.lc.activiti.sys.entity.SysDept;
import com.lc.activiti.sys.service.SysDeptService;
import com.lc.activiti.utils.TreeUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 组织架构(SysDept)表服务实现类
 *
 * @author 骆超
 * @since 2021-07-02 16:21:09
 */
@Service("sysDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDept> implements SysDeptService {

    @Override
    public Result saveDeptInfo(SysDept sysDept) {
        return Result.success(sysDept.insertOrUpdate());
    }

    @Override
    public Result getDeptTree() {
        return  Result.success(TreeUtil.listToTree(JSONUtil.parseArray(list()),new TreeSetting()
                .setSonColumn("id")
                .setParColumn("superior")
                .setChildrenColumn("children")
                .setChangeColumn(new JSONObject().set("title","name"))
                .setDefaultValues(new JSONObject().set("expand",true))
                .setName("name")
                .setParentName("superiorName")
        ));
    }

    @Override
    public Result getExceptChildTree(SysDept sysDept) {
        return  Result.success(TreeUtil.listToTree(sysDept.getId(),JSONUtil.parseArray(list()),new TreeSetting()
                .setSonColumn("id")
                .setParColumn("superior")
                .setChildrenColumn("children")
                .setChangeColumn(new JSONObject().set("title","name"))
                .setDefaultValues(new JSONObject().set("expand",true))
        ));
    }
}
